package utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Date {
	private static Pattern DATE_PATTERN = Pattern.compile("^(\\d*)\\-(\\d*)\\-(\\d*)$");
	
	public int month = 0;
	public int day = 0;
	public int year = 0;
	
	public String toString() {
		return Integer.toString(month) + "-" + Integer.toString(day) + "-" + Integer.toString(year);
	}
	
	public static Date parseDateString(String str) {
		Date date = new Date();
		
		boolean is_date = false;
		
		Matcher date_matcher = DATE_PATTERN.matcher(str);
		if (!date_matcher.matches()) {
			return null;
		}
		String month_str = date_matcher.group(1);
		if (!month_str.equals("")) {
			date.month = Integer.parseInt(month_str);
			is_date = true;
		}
		String day_str = date_matcher.group(2);
		if (!day_str.equals("")) {
			date.day = Integer.parseInt(day_str);
			is_date = true;
		}
		String year_str = date_matcher.group(3);
		if (!year_str.equals("")) {
			date.year = Integer.parseInt(year_str);
			is_date = true;
		}
		
		if (!is_date)
			return null;
		return date;
	}
	
	public static Date parseKBDate(String str) {
		Date date = new Date();
		
		boolean is_date = false;
		Matcher date_matcher = DATE_PATTERN.matcher(str);
		if (!date_matcher.matches()) {
			return null;
		}
		String month_str = date_matcher.group(2);
		if (!month_str.equals("")) {
			date.month = Integer.parseInt(month_str);
			is_date = true;
		}
		String day_str = date_matcher.group(3);
		if (!day_str.equals("")) {
			date.day = Integer.parseInt(day_str);
			is_date = true;
		}
		String year_str = date_matcher.group(1);
		if (!year_str.equals("")) {
			date.year = Integer.parseInt(year_str);
			is_date = true;
		}
		
		if (!is_date)
			return null;
		return date;
	}
	
	public static Date parseTextDate(String str) {
		Date date = new Date();
		
		boolean is_date = false;
		
		Matcher date_matcher = DATE_PATTERN.matcher(str);
		if (!date_matcher.matches()) {
			return null;
		}
		String month_str = date_matcher.group(1);
		if (!month_str.equals("")) {
			date.month = Integer.parseInt(month_str);
			is_date = true;
		}
		String day_str = date_matcher.group(2);
		if (!day_str.equals("")) {
			date.day = Integer.parseInt(day_str);
			is_date = true;
		}
		String year_str = date_matcher.group(3);
		if (!year_str.equals("")) {
			date.year = Integer.parseInt(year_str);
			is_date = true;
		}
		
		if (!is_date)
			return null;
		return date;
	}
}
